﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Hired
{
    /*  Comment will be found under a Thread.   */
    // !NOTE -- posterID must be added
    // !NOTE -- this is because it does not have FK
    public class DBComment
    {
        //Add a Comment
        public static void AddComment(Thread t, Comment c)
        {
            c.CommentID = "tempvalue";
            using (hireddbEntities db = new hireddbEntities())
            {
                db.Attach(t);
                t.Comments.Add(c);
                db.SaveChanges();
            }
        }

        //Retrieve a Comment
        public static Comment retrieveComment(string commentID)
        {
            Comment comment = null;

            using (hireddbEntities db = new hireddbEntities())
            {
                    comment =
                    (from c in db.Comments
                    where c.CommentID == commentID
                    select c).SingleOrDefault();
            }
            return comment;
        }

        //Update a Comment
        public static bool updateComment(Comment c)
        {
            try
            {
                using (hireddbEntities db = new hireddbEntities())
                {
                    Comment commentToUpdate = retrieveComment(c.CommentID);
                    db.Attach(commentToUpdate);
                    commentToUpdate.Contents = c.Contents;
                    commentToUpdate.Rating = c.Rating;
                    commentToUpdate.Timestamp = commentToUpdate.Timestamp;

                    db.SaveChanges();
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
        //Delete a Comment
        public static void deleteComment(Comment c)
        {
            Comment commentToDelete = retrieveComment(c.CommentID);

            using (hireddbEntities db = new hireddbEntities())
            {
                db.Attach(commentToDelete);
                db.DeleteObject(commentToDelete);
                db.SaveChanges();
            }
        }
        public static void deleteCommentById(string id)
        {
            Comment commentToDelete = retrieveComment(id);

            using (hireddbEntities db = new hireddbEntities())
            {
                db.Attach(commentToDelete);
                db.DeleteObject(commentToDelete);
                db.SaveChanges();
            }
        }
    }
}